package utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


//参考资料：http://www.runoob.com/java/java-sending-email.html


public class SendMailUtil {
	// 发件人的电子邮件
	private String from = "test_2018_1@163.com";
	// SMTP服务器地址
	private String host = "smtp.163.com";
	// 用户名   
	private String username = "test_2018_1@163.com";
	// 用户密码
	private String userpass = "qzk2018";
	
	public SendMailUtil() {
		from = "test_2018_1@163.com";
		host = "smtp.163.com";
		username = "test_2018_1@163.com";
		userpass = "qzk2018";
	}
	
	/**
	 * 设置对象基本属性
	 * @param fromAdd 发送方电子邮件地址
	 * @param smtpHost SMTP主机地址
	 * @param userName 用户名
	 * @param userPass 用户密码
	 */
	public void setProperties(String fromAdd, String smtpHost, String userName, String userPass) {
		this.from = fromAdd;
		this.host = smtpHost;
		this.username = userName;
		this.userpass = userPass;
	}
	
	
	/**
	 * 发送一封电子邮件
	 * @param toAddress 目标电子邮件地址
	 * @param subject 主题
	 * @param body 内容
	 * @return 执行信息
	 */
	public final String sendMail(String toAddress, String subject, String body) {
		   // 收件人的电子邮件
		   String result = "";
		   String to = toAddress;
		   
		   // 获取系统属性对象
		   Properties properties = System.getProperties();

		   // 设置邮件服务器
		   properties.setProperty("mail.smtp.host", host);
		   properties.setProperty("mail.user", username);
		   properties.setProperty("mail.password", userpass);
		   properties.setProperty("mail.smtp.auth", "true");
		   Authentication authentication = new Authentication(username, userpass);

		   // 获取默认的Session对象。
		   Session mailSession = Session.getDefaultInstance(properties, authentication);

		   try{
		      // 创建一个默认的MimeMessage对象。
		      MimeMessage message = new MimeMessage(mailSession);
		      // 设置 From: 头部的header字段
		      message.setFrom(new InternetAddress(from));
		      // 设置 To: 头部的header字段
		      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		      // 设置 Subject: header字段
		      message.setSubject(subject);
		      // 现在设置的实际消息
		      message.setText(body);
		      // 发送消息
		      Transport.send(message);
		      result = "Mail successfully send";
		   }catch (MessagingException mex) {
		      mex.printStackTrace();
		      result = "An error has occured";
		   }
		   
		   return result;
	}
	
	//用于处理部分邮件服务器要求验证所用的内部类

	class Authentication extends Authenticator{  
	      String username=null;  
	      String password=null;  

	      public Authentication(){  
	      }  
	      public Authentication(String username, String password) {  
	      this.username = username;  
	      this.password = password;  
	      }  
	      protected PasswordAuthentication getPasswordAuthentication(){
	      PasswordAuthentication pa = new PasswordAuthentication(username, password);
	      return pa;
	      }  
	}
}
